<template>
  <div class="app-container">
    <!--        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="应急救援物资种类" prop="erCategory">
                <el-input
                  v-model="queryParams.erCategory"
                  placeholder="请输入应急救援物资种类"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="现有数量" prop="erNowCount">
                <el-input
                  v-model="queryParams.erNowCount"
                  placeholder="请输入现有数量"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="所需数量" prop="erAvailableCount">
                <el-input
                  v-model="queryParams.erAvailableCount"
                  placeholder="请输入所需数量"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="经度" prop="erLg">
                <el-input
                  v-model="queryParams.erLg"
                  placeholder="请输入经度"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="纬度" prop="erLt">
                <el-input
                  v-model="queryParams.erLt"
                  placeholder="请输入纬度"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="保管人" prop="erSpName">
                <el-input
                  v-model="queryParams.erSpName"
                  placeholder="请输入保管人"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="保管人联系方式" prop="erSpPhone">
                <el-input
                  v-model="queryParams.erSpPhone"
                  placeholder="请输入保管人联系方式"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:rescue:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:rescue:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:rescue:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:rescue:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rescueList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <!--      <el-table-column align="center" label="化学品ID" prop="erId"/>-->
      <el-table-column align="center" label="应急救援物资" prop="erCategory"/>
      <el-table-column align="center" label="现有数量" prop="erNowCount"/>
      <el-table-column align="center" label="所需数量" prop="erAvailableCount"/>
      <!--      <el-table-column align="center" label="经度" prop="erLg"/>
            <el-table-column align="center" label="纬度" prop="erLt"/>-->
      <el-table-column align="center" label="保管人" prop="erSpName"/>
      <el-table-column align="center" label="保管人联系方式" prop="erSpPhone"/>
      <el-table-column align="center" label="应急响应人" prop="erHlName"/>
      <el-table-column align="center" label="响应人联系方式" prop="erHlPhone"/>
      <!--      <el-table-column align="center" label="备注" prop="remark"/>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['safe:rescue:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['safe:rescue:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改应急物资和人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="60%">
      <el-form ref="form" :model="form" :rules="rules" class="clearfix  m-f-a-i" label-width="120px">
        <div class="content-left" style="width: 45%">
          <el-form-item label="应急救援物资" prop="erCategory">
            <div style="width: 100%;">
              <el-select v-model="ercIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="ercChange">
                <el-option v-for="item in erc" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>

          <el-form-item label="现有数量" prop="erNowCount" type="number">
            <el-input v-model="form.erNowCount" placeholder="请输入现有数量"/>
          </el-form-item>
          <el-form-item label="保管人" prop="erSpName">
            <el-input v-model="form.erSpName" placeholder="请输入保管人"/>
          </el-form-item>
          <el-form-item label="应急响应人" prop="erHlName">
            <el-input v-model="form.erHlName" placeholder="请输入应急响应人"/>
          </el-form-item>
        </div>
        <div class="content-left" style="float: right; width: 45%">
          <el-form-item label="经纬度">
            <el-row>
              <el-col>
                <div style="display: flex">
                  <div style="width: 38%">
                    <el-input v-model="form.erLg" placeholder="请输入经度"/>
                  </div>
                  <div style="width: 38%">
                    <el-input v-model="form.erLt" placeholder="请输入纬度"/>
                  </div>
                </div>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="所需数量" prop="erAvailableCount" type="number">
            <el-input v-model="form.erAvailableCount" placeholder="请输入所需数量"/>
          </el-form-item>
          <el-form-item label="保管人联系方式" prop="erSpPhone">
            <el-input v-model="form.erSpPhone" placeholder="请输入保管人联系方式"/>
          </el-form-item>
          <el-form-item label="响应人联系方式" prop="erHlPhone">
            <el-input v-model="form.erHlPhone" placeholder="请输入响应人联系方式"/>
          </el-form-item>


        </div>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addRescue, delRescue, exportRescue, getRescue, listRescue, updateRescue} from "@/api/safe/rescue";
import {getToken} from "@/utils/auth";
import {InfoItem} from "@/api/enterprise/info";
import {isIntegerNotMust,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Rescue",
  data() {
    return {
      erc: [
        {
          value: '0',
          label: '个人防护装备器材'
        },
        {
          value: '1',
          label: '消防设施'
        },
        {
          value: '2',
          label: '堵漏、收集器材/设备'
        },
        {
          value: '3',
          label: '应急救援物资'
        },
        {
          value: '4',
          label: '其他'
        },
      ],
      ercIndex: '0',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应急物资和人员表格数据
      rescueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        erCategory: null,
        erNowCount: null,
        erAvailableCount: null,
        erLg: null,
        erLt: null,
        erSpName: null,
        erSpPhone: null,
        erHlName: null,
        erHlPhone: null,
        epId:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        erCategory: [
          {required: true, message: "应急救援物资不能为空", trigger: "blur"}
        ],
        erSpPhone: [
          {validator:validatePhoneTwo, trigger: "blur"}
        ],
        erHlPhone: [
          {validator:validatePhoneTwo, trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {


    ercChange(e) {
      this.form.erCategory = e;
    },
    /** 查询应急物资和人员列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listRescue(this.queryParams).then(response => {
            this.rescueList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        erId: null,
        erCategory: null,
        erNowCount: null,
        erAvailableCount: null,
        erLg: null,
        erLt: null,
        erSpName: null,
        erSpPhone: null,
        erHlName: null,
        erHlPhone: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.erId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应急物资和人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const erId = row.erId || this.ids
      getRescue(erId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应急物资和人员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.erId != null) {
            updateRescue(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addRescue(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const erIds = row.erId || this.ids;
      this.$confirm('是否确认删除应急物资和人员编号为"' + erIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delRescue(erIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有应急物资和人员数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportRescue(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
